package com.ncloud.monitor.zabbix4j.itservice;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.monitor.zabbix4j.ZabbixApiException;
import com.ncloud.monitor.zabbix4j.ZabbixApiMethod;

/**
 * @author Suguru Yajima
 */
public class ITService extends ZabbixApiMethod {

    public ITService(String apiUrl, String auth) {
        super(apiUrl, auth);
    }

    public ITServiceCreateResponse create(ITServiceCreateRequest request) throws ZabbixApiException {
        ITServiceCreateResponse response = null;
        request.setAuth(auth);

        JSONObject requestJson = JSON.parseObject(JSON.toJSONString(request));

        try {
            String responseJson = sendRequest(requestJson);

            response = JSON.parseObject(responseJson, ITServiceCreateResponse.class);
        } catch (ZabbixApiException e) {
            throw new ZabbixApiException(e);
        }

        return response;
    }

    public ITServiceDeleteResponse delete(ITServiceDeleteRequest request) throws ZabbixApiException {
        ITServiceDeleteResponse response = null;
        request.setAuth(auth);

        JSONObject requestJson = JSON.parseObject(JSON.toJSONString(request));

        try {
            String responseJson = sendRequest(requestJson);

            response = JSON.parseObject(responseJson, ITServiceDeleteResponse.class);
        } catch (ZabbixApiException e) {
            throw new ZabbixApiException(e);
        }

        return response;
    }

    public ITServiceUpdateResponse update(ITServiceUpdateRequest request) throws ZabbixApiException {
        ITServiceUpdateResponse response = null;
        request.setAuth(auth);

        JSONObject requestJson = JSON.parseObject(JSON.toJSONString(request));

        try {
            String responseJson = sendRequest(requestJson);

            response = JSON.parseObject(responseJson, ITServiceUpdateResponse.class);
        } catch (ZabbixApiException e) {
            throw new ZabbixApiException(e);
        }

        return response;
    }

    public ITServiceGetResponse get(ITServiceGetRequest request) throws ZabbixApiException {
        ITServiceGetResponse response = null;
        request.setAuth(auth);

        JSONObject requestJson = JSON.parseObject(JSON.toJSONString(request));

        try {
            String responseJson = sendRequest(requestJson);

            response = JSON.parseObject(responseJson, ITServiceGetResponse.class);
        } catch (ZabbixApiException e) {
            throw new ZabbixApiException(e);
        }

        return response;
    }
}
